﻿using UnityEngine;
using System.Collections.Generic;
using LitJson;
using UnityEngine.UI;

//本地玩家输入
public class LocalController : MonoBehaviour
{

    protected Player localPlayer;
    double time = 0;
    Transform image;
    Vector2 image_Vec2;
    public void SetLocol(Player value)
    {
        localPlayer = value;
        value.gameObject.transform.GetChild(1).gameObject.SetActive(true);
        image = value.transform.GetChild(3).transform.GetChild(0);
        image_Vec2 = image.GetComponent<RectTransform>().sizeDelta;
        Debug.Log(image);
    }
    void Update()
    {
        if (localPlayer)
        {
            float dx = Input.GetAxis("Horizontal");
            float dy = Input.GetAxis("Vertical");

            Vector3 dir = new Vector3(0, 0, dy);
            Vector3 rota = new Vector3(0, 10 * dx, 0);
            if (!(dir.z == 0))
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic["username"] = localPlayer.userData.username;
                dic["dir"] = JsonUtility.ToJson(dir);

                NetworkManager.Instance.SendMessage(Protocol.PlayerMove_CMD, JsonMapper.ToJson(dic));
            }
            if (!(rota.y == 0))
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic["username"] = localPlayer.userData.username;
                dic["rota"] = JsonUtility.ToJson(rota);

                NetworkManager.Instance.SendMessage(Protocol.PlayerRota_CMD, JsonMapper.ToJson(dic));
            }
            localPlayer.Move(dir);
            localPlayer.Rota(rota);




            if (Input.GetKey(KeyCode.Space))
            {
                Debug.Log("准备开炮");
                image.gameObject.SetActive(true);
                time += Time.deltaTime;
                if (time>5)
                {
                    time = 5;
                }
                Vector2 ve= image_Vec2;
                ve.y += (float)time * 2;
                image.gameObject.GetComponent<RectTransform>().sizeDelta = ve;

                //Rect re = image.rectTransform.rect;
                //re.height = (float)time;

            }
            if (Input.GetKeyUp(KeyCode.Space))
            {
                image.gameObject.SetActive(false);

                Debug.Log("开炮！");
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic["username"] = localPlayer.userData.username;
                Debug.Log(time);
                //string json = JsonMapper.ToJson(time);
                //Debug.Log(json);
                dic["fireTime"] = time.ToString();
                NetworkManager.Instance.SendMessage(Protocol.PlayerFire_CMD, JsonMapper.ToJson(dic));

                localPlayer.Fire(time);
                time = 0;
            }

        }
    }
}
